package com.bestcem.xm.ticket.controller.vo.ticket;

import com.bestcem.xm.ticket.entity.mongo.TicketRecord;
import com.bestcem.xm.ticket.enums.TicketRecordEventEnum;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import javax.annotation.Nullable;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/9 23:50
 */
@Data
public class TicketRecordVO {

    @JsonProperty(value = "id")
    private String id;

    /**
     * orgId
     */
    @JsonProperty(value = "orgId")
    private String orgId;

    /**
     * userId
     */
    @JsonProperty(value = "userId")
    private String userId;

    /**
     * title
     */
    @JsonProperty(value = "title")
    private String title;

    /**
     * ticketId
     */
    @JsonProperty(value = "ticketId")
    private String ticketId;

    /**
     * roleId
     */
    @JsonProperty(value = "roleId")
    private String roleId;

    /**
     * region
     */
    @JsonProperty(value = "region")
    private String region;

    /**
     * regionName
     */
    @JsonProperty(value = "regionName")
    private String regionName;

    /**
     * userName
     */
    @JsonProperty(value = "userName")
    private String userName;

    /**
     * source
     *
     * @see com.bestcem.xm.ticket.enums.TicketRecordSourceEnum
     */
    @JsonProperty(value = "source")
    private Integer source;

    /**
     * event
     *
     * @see TicketRecordEventEnum
     */
    @JsonProperty(value = "event")
    private Integer event;

    /**
     * custom_attr
     */
    @JsonProperty(value = "custom_attr")
    private CustomAttrVO customAttr;

    /**
     * surveyId
     */
    @JsonProperty(value = "surveyId")
    private String surveyId;

    @JsonProperty(value = "created_id")
    private String createdId;

    @JsonProperty(value = "created_dt")
    private Date createdDate;

    @JsonProperty(value = "updated_id")
    private String updatedId;

    @JsonProperty(value = "updated_dt")
    private Date updatedDate;

    /**
     * 数据记录标记
     */
    @JsonProperty(value = "record_flag")
    private Integer recordFlag = 1;

    /**
     * 转派对象,even为15时存在（2.3版新增）
     */
    @JsonProperty(value = "transfer_username")
    private String transferUsername;

    /**
     * 转派/关闭/重新打开原因（2.3版新增）
     */
    @JsonProperty(value = "reason")
    private String reason;

    @JsonProperty(value = "ticketStatus")
    private Integer status;


    /**
     * 当前工单状态（1-处理中，2-已关闭，3-处理完成，4-处理失败，5-无需跟进）（2.3版新增）
     */
    @JsonProperty(value = "ticket_status")
    private Integer ticketStatus;

    // ======================================================

    /**
     * 工单进度自定义参数, 分为两种 跟进/审核
     *
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2021/8/18 15:35
     */
    @NoArgsConstructor
    @Data
    public static class CustomAttrVO implements Serializable {
        private static final long serialVersionUID = 2808726620840986656L;

        // ==== 公共参数 ====
        /**
         * content
         */
        @JsonProperty(value = "content")
        private String content;

        // ==== 跟进参数 ====
        /**
         * isCompleted
         */
        @JsonProperty(value = "isCompleted")
        private Boolean isCompleted;

        /**
         * custom_fields
         */
        @JsonProperty(value = "custom_fields")
        private List<CustomFieldVO> customFields;

        @Nullable
        public static CustomAttrVO getInstance(TicketRecord.CustomAttr entity) {
            if (Objects.isNull(entity)) {
                return null;
            }
            CustomAttrVO data = new CustomAttrVO();
            data.setIsCompleted(entity.getIsCompleted());

            List<CustomFieldVO> customFieldList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(entity.getCustomFields())) {
                List<TicketRecord.CustomAttr.CustomField> customFields = entity.getCustomFields();
                for (TicketRecord.CustomAttr.CustomField customField : customFields) {
                    customFieldList.add(CustomFieldVO.getInstance(customField));
                }
            }
            // 兼容历史工单日志数据
            if (StringUtils.isNotBlank(entity.getContent())) {
                customFieldList.add(new CustomFieldVO(null, "处理记录", entity.getContent()));
            }
            data.setCustomFields(customFieldList);

            return data;
        }

        @NoArgsConstructor
        @AllArgsConstructor
        @Data
        public static class CustomFieldVO implements Serializable {
            private static final long serialVersionUID = 2808726620840986656L;

            /**
             * field
             */
            private String field;

            /**
             * text
             */
            private String text;

            /**
             * value
             */
            private String value;

            @Nullable
            public static CustomFieldVO getInstance(TicketRecord.CustomAttr.CustomField entity) {
                if (Objects.isNull(entity)) {
                    return null;
                }
                CustomFieldVO data = new CustomFieldVO();
                data.setField(entity.getField());
                data.setText(entity.getText());
                data.setValue(entity.getValue());
                return data;
            }
        }
    }
}
